<?php

namespace App\Admin\Controllers;

use App\Models\Order;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;
use Encore\Admin\Widgets\Box;
use Encore\Admin\Widgets\Table;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class OrderController extends Controller
{
    use ModelForm;
    protected $header = '订单列表';
    /**
     * Index interface.
     *
     * @return Content
     */
    public function index(Request $request)
    {
        return Admin::content(function (Content $content) use ($request) {

            $content->header($this->header);
            $content->description('description');

            $content->body($this->grid($request));
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid(Request $request)
    {
        return Admin::grid(Order::class, function (Grid $grid) use ($request) {

            $grid->model()->orderBy('created_at','desc');

            $grid->id('id');
            $grid->order_number('订单编号');
            $grid->created_at('提交时间');
            $grid->user()->username('用户名称');
            $grid->user()->mobile('手机号');
            $grid->order_amount('消费积分');
            $grid->pay_type('支付方式')->using(['1'=>'积分兑换']);
            $grid->order_status('订单状态')->using(['1'=>'待支付','2'=>'已支付']);

            //去除创建按钮
            $grid->disableCreateButton();

            $grid->actions(function (Grid\Displayers\Actions $actions) {
                $actions->disableEdit();
                $actions->disableView();
                $actions->disableDelete();

                $actions->append('<a href="'.$actions->getResource().'/'.$actions->getKey().'"><i class="fa fa-eye"></i>查看订单</a>');

            });

            //搜索栏
            $grid->filter(function (Grid\Filter $filter) {
                $filter->like('order_number','订单编号');
                $filter->equal('order_status', '订单状态')->select([
                    '1'=>'待支付','2'=>'已支付'
                ]);
                $filter->like('user.username', '用户名称');
                $filter->like('user.mobile', '手机号');

            });

        });
    }

    /**
     * Display the specified resource.
     *
     * @param  int $id
     * @return Content
     */
    public function show($id, Content $content)
    {
        $content->header('订单详情');

        $data = Order::with('goods')
            ->with('user')
            ->where('id',$id)
            ->first()->toArray();

        $content->row(view('admin.back'));

        if ($data){

            $express = '未发货';
            /*if (isset($data['express']['express_sn']) && $data['express']['express_sn']){
                $express = $data['express']['express_sn'];
            }*/

            $headers = ['订单编号', '物流单号', '用户账号', '支付方式', '支付积分'];
            $rows = [
                [$data['order_number'], $express , @$data['user']['username'], '积分兑换', $data['order_amount']],
            ];
            $table = new Table($headers, $rows);
            $content->row((new Box('基本信息', $table))->solid()->style('primary'));

            $content->row(view('admin.order.goods',compact('data')));

        } else {
            abort(404);
        }

        return $content;

    }


    
}
